package com.kobeliu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.kobeliu.api.feign.UsersMicroServiceFeign;
import com.kobeliu.base.BaseInfoProperties;
import com.kobeliu.enums.AggsUserType;
import com.kobeliu.enums.UserRole;
import com.kobeliu.exceptions.GraceException;
import com.kobeliu.mapper.UsersMapper;
import com.kobeliu.pojo.Users;
import com.kobeliu.pojo.bo.ModifyUserBO;
import com.kobeliu.pojo.bo.ReviewCompanyBO;
import com.kobeliu.pojo.eo.AggsUserEO;
import com.kobeliu.pojo.vo.UsersVO;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.result.ResponseStatusEnum;
import com.kobeliu.service.UserService;
import com.kobeliu.utils.LocalDateUtils;
import com.kobeliu.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class UserServiceImpl extends BaseInfoProperties implements UserService {

    @Autowired
    private UsersMapper usersMapper;


    @Transactional
    @Override
    public void modifyUserInfo(ModifyUserBO bo) {

        String userId = bo.getUserId();
        if(StringUtils.isNotBlank(userId)){

            Users users = new Users();
            users.setId(userId);
            users.setUpdatedTime(LocalDateTime.now());

            BeanUtils.copyProperties(bo,users);
            usersMapper.updateById(users);

        }else{
            GraceException.display(ResponseStatusEnum.USER_UPDATE_ERROR);
        }


    }

    @Override
    public Users getUserById(String userId) {

        Users users = usersMapper.selectById(userId);
        if(users !=null){
            return users;
        }
        GraceException.display(ResponseStatusEnum.USER_STATUS_ERROR);
        return null;
    }

    @Override
    public Long getCountByCompanyId(String companyId) {
        return usersMapper.selectCount(new QueryWrapper<Users>()
                .eq("hr_in_which_company_id",companyId));
    }

    @Transactional
    @Override
    public void updateUserCompanyId(String userId, String realName, String companyId) {
        Users HRUser = new Users();
        HRUser.setId(userId);
        HRUser.setRealName(realName);
        HRUser.setHrInWhichCompanyId(companyId);

        HRUser.setUpdatedTime(LocalDateTime.now());

        usersMapper.updateById(HRUser);
    }

    @Transactional
    @Override
    public void updateUser2Hr(String hrUserId) {

        Users hrUser = new Users();
        hrUser.setId(hrUserId);
        hrUser.setRole(UserRole.RECRUITER.type);
        hrUser.setUpdatedTime(LocalDateTime.now());

        usersMapper.updateById(hrUser);
    }

    @Override
    public PagedGridResult selectHrListByCompanyId(String companyId,Integer page,Integer limit) {

        PageHelper.startPage(page,limit);

        List<Users> users = usersMapper.selectList(new QueryWrapper<Users>()
                .eq("hr_in_which_company_id", companyId));

        return setterPagedGrid(users,page);

    }

    @Transactional
    @Override
    public void changeUserToCand(String hrUserId) {

        Users users = new Users();

        users.setId(hrUserId);
        users.setRole(UserRole.CANDIDATE.type);
        users.setHrInWhichCompanyId("0");
        users.setUpdatedTime(LocalDateTime.now());

        usersMapper.updateById(users);


    }

    @Override
    public List<UsersVO> getList(List<String> userIds) {

        List<UsersVO> vos = new ArrayList<>();

        if(userIds.size() == 0){
            return vos;
        }

        List<Users> users = usersMapper.selectList(new QueryWrapper<Users>()
                .in("id", userIds));

        return users.stream().map(obj ->{
            UsersVO v= new UsersVO();
            BeanUtils.copyProperties(obj,v);
            return v;
        }).collect(Collectors.toList());
    }

}
